package com.sms.biz.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.sms.biz.IRouteManageBiz;
import com.sms.dao.IRouteManageDAO;
import com.sms.po.TRoute;


/**
 * @author 20111229 by cxh
 */
public class RouteManageBizImpl implements IRouteManageBiz {
	private IRouteManageDAO irbd = null;
	public void setIrbd(IRouteManageDAO irbd){ this.irbd = irbd; }

	@Override
	public List<TRoute> showAllRouteByPage(int start, int limit) {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria
				.forClass(TRoute.class);
		if (start <= 0) {start = 0;		}
		if (limit <= 0) {limit = 10;	}
		return irbd.showAllRouteByPage(criteria, start, limit);
	}
	@Override
	public int getAllRouteCount() {
		// 离线查询
		DetachedCriteria criteria = DetachedCriteria
				.forClass(TRoute.class);
		return irbd.getAllRouteCount(criteria);
	}
	@Override
	public boolean addRoute(TRoute tr) {
		try {
			irbd.addRoute(tr);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	@Override
	public boolean removeRoute(TRoute tr) {
		try {
			irbd.removeRoute(tr);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	@Override
	public boolean updateRoute(TRoute tr) {
		try {
			irbd.updateRoute(tr);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public int getDefaultRoute(int routeId){
		int reRouteId = routeId;
		List<TRoute> userRouteList = irbd.getRouteByCondition(" where status=1 and id=" + routeId);
		if(userRouteList.isEmpty())
		{
			List<TRoute> systemRouteList = irbd.getRouteByCondition(" where flag=1 and status=1");
			if(!systemRouteList.isEmpty())
			{
				TRoute systemRoute = systemRouteList.get(0);
				reRouteId = systemRoute.getId();
			}
		}else{
			reRouteId = userRouteList.get(0).getId();
		}
		return reRouteId;
	}
}
